package sasp_messages;

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;

public class SASP_RSA {

	private PrivateKey privKey;
	private Signature privSig;

	public byte[] decrypt(String _c) throws InvalidKeyException, SignatureException	{
		privSig.initSign(privKey);
		privSig.update(_c.getBytes());
		return privSig.sign();
	}

	public byte[] encrypt(String _m, PublicKey _pubKey) throws InvalidKeyException, SignatureException, NoSuchAlgorithmException	{
                Signature pubSig = Signature.getInstance("RSA");
		/*
                pubSig.initVerify(_pubKey);
		pubSig.update(_m.getBytes());
		return pubSig.verify();
                */
                pubSig.initSign((PrivateKey) _pubKey);
                pubSig.update(_m.getBytes());
                return pubSig.sign();
	}
	
	public SASP_RSA() throws NoSuchAlgorithmException	{
		
		/*
		Throw away key pair until we can import our own
		*/
                KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
                KeyPair rsaKeys = kpg.generateKeyPair();

                privSig = Signature.getInstance("RSA");
		privKey = rsaKeys.getPrivate();
	}



}
